//
//  SettingView.swift
//  jiweiapp
//
//  Created by Kin Wong on 15/8/13.
//  Copyright (c) 2015年 Kin Wong. All rights reserved.
//
//代码注释参考MainView.swift
import UIKit

class MoreView: UIViewController, UITableViewDataSource, UITableViewDelegate {

    var moreView: UITableView?
    var moreItems: [String]?
    
    override func loadView() {
        super.loadView()
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.view.backgroundColor = UIColor.whiteColor()
        self.moreItems = NSArray(contentsOfFile: NSBundle.mainBundle().pathForResource("More", ofType: "xml")!) as? Array
        self.moreView = UITableView(frame: CGRectMake(0, 0, CGRectGetWidth(self.view.bounds), CGRectGetHeight(self.view.bounds)-60), style: UITableViewStyle.Plain)
        self.moreView!.delegate = self
        self.moreView!.dataSource = self
        self.moreView!.registerClass(UITableViewCell.self, forCellReuseIdentifier: "MoreCell")
        self.moreView!.tableFooterView = UIView(frame: CGRectZero)
        self.view.addSubview(moreView!)

        // Do any additional setup after loading the view.
    }
    
    func numberOfSections(tableView: UITableView!) -> Int {
        return 1
    }
    
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.moreItems!.count
    }
    
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let id: String = "MoreCell"
        let cell = tableView.dequeueReusableCellWithIdentifier(id, forIndexPath: indexPath) as! UITableViewCell
        
        cell.accessoryType = UITableViewCellAccessoryType.DisclosureIndicator
        cell.textLabel?.text = self.moreItems![indexPath.row]
        
        return cell
    }
    
    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        self.moreView!.deselectRowAtIndexPath(indexPath, animated: true)
        
        switch indexPath.row {
        case 0: personalView()
        case 1: contactView(indexPath.row)
        case 2: settingView(indexPath.row)
        case 3: aboutView()
        default: break
        }
    }
    
    func personalView() {
        let alert = UIAlertView()
        alert.title = "告诉你个秘密"
        alert.message = "这东西我没写！怎么？不爽？来咬我啊！"
        alert.addButtonWithTitle("哼！")
        alert.show()
    }
    
    func contactView(row: Int) {
        let contactView = Contacts()
        contactView.title = self.moreItems?[row]
        self.navigationController?.pushViewController(contactView, animated: true)
    }
    
    func settingView(row: Int) {
        let settingView = SettingView()
        settingView.title = self.moreItems?[row]
        self.navigationController?.pushViewController(settingView, animated: true)
    }
    
    func aboutView() {
        let aboutView = About()
        self.presentViewController(aboutView, animated: true, completion: nil)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

}
